<template>
  <el-card class="box-card" shadow="always">
    <div slot="header" class="clearfix">
      <span>{{ title }}</span>
      <el-button-group style="float: right;">
        <el-button
          v-if="!isShow"
          size="mini"
          icon="el-icon-plus"
          round
          :loading="loadingOptions.loading"
          :disabled="loadingOptions.isDisabled"
          @click="!data.id ? submitForm() : updateData()"
        >{{
          loadingOptions.loadingText }}</el-button>
        <el-button size="mini" icon="el-icon-back" round @click="showCard">返回</el-button>
      </el-button-group>
    </div>
    <div class="body-wrapper">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px" :disabled="isShow">
        <el-form-item label="AppName" prop="appName">
          <el-input v-model="form.appName" placeholder="AppName" />
        </el-form-item>
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入执行器名称" />
        </el-form-item>
        <el-form-item label="排序" prop="prop">
          <el-input v-model="form.orderNumber" placeholder="请输入执行器序号" />
        </el-form-item>
        <el-form-item label="注册方式" prop="addressType">
          <el-radio-group v-model="form.addressType">
            <el-radio :label="0">自动注册</el-radio>
            <el-radio :label="1">手动录入</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="机器地址" prop="addressList">
          <el-input v-model="form.addressList" placeholder="请输入,多个以逗号分隔" />
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
// import * as user from '@/api/integration/datax/datax-resource'
import * as executor from '@/api/integration/datax/datax-executor'

export default {
  name: 'ExecutorAdd',
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    },
    title: { type: String, default: '新增资源' },
    isShow: { type: Boolean, default: false }
  },
  data() {
    return {
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showAdd: false,
        showEdit: false,
        showDetail: false
      },
      // 表单校验
      rules: {
        appName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '不能为空', trigger: 'blur' }],
        order: [{ required: true, message: '不能为空', trigger: 'blur' }],
        addressType: [{ required: true, message: '不能为空', trigger: 'change' }]
      },
      // 保存按钮
      loadingOptions: {
        loading: false,
        loadingText: '保存',
        isDisabled: false
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    if (this.data.id) {
      this.getResourceData(this.data.id)
    }
  },
  methods: {
    showCard() {
      this.$emit('showCard', this.showOptions)
    },
    getResourceData(id) {
      executor.getExecutorDetail(id).then(response => {
        if (response.success) {
          this.form = response.data
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loadingOptions.loading = true
          this.loadingOptions.loadingText = '保存中...'
          this.loadingOptions.isDisabled = true
          executor.created(this.form).then(response => {
            if (response.success) {
              this.$message.success('保存成功')
              setTimeout(() => {
                // 2秒后跳转列表页
                this.$emit('showCard', this.showOptions)
              }, 2000)
            } else {
              this.$message.error('保存失败')
              this.loadingOptions.loading = false
              this.loadingOptions.loadingText = '保存'
              this.loadingOptions.isDisabled = false
            }
          }).catch(() => {
            this.loadingOptions.loading = false
            this.loadingOptions.loadingText = '保存'
            this.loadingOptions.isDisabled = false
          })
        }
      })
    },
    // 更新数据
    updateData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.form)
          executor.updated(tempData).then((response) => {
            if (response.success) {
              this.$message.success('保存成功')
              setTimeout(() => {
                // 2秒后跳转列表页
                this.$emit('showCard', this.showOptions)
              }, 2000)
            } else {
              this.$message.error('保存失败')
              this.loadingOptions.loading = false
              this.loadingOptions.loadingText = '保存'
              this.loadingOptions.isDisabled = false
            }
          }).catch(() => {
            this.loadingOptions.loading = false
            this.loadingOptions.loadingText = '保存'
            this.loadingOptions.isDisabled = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card ::v-deep .el-card__body {
  height: calc(100vh - 230px);
  overflow-y: auto;
}
</style>
